package com.xckj.teacher.ui.activity;

import android.content.Context;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;

import com.xckj.teacher.R;
import com.xckj.teacher.bean.SearchBookBean;
import com.xckj.teacher.databinding.ActivitySearchBookBinding;
import com.xckj.teacher.framework.BaseActivity;
import com.xckj.teacher.global.Constant;
import com.xckj.teacher.iview.ISearchBookView;
import com.xckj.teacher.presenter.SearchBookPresenter;
import com.xckj.teacher.ui.adapter.SearchBookResultAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ChenYao
 * @Describe: 搜索书籍
 * @Date: 2024/4/12 15:02
 */
public class SearchBookActivity extends BaseActivity<ActivitySearchBookBinding> implements ISearchBookView {
    private SearchBookPresenter searchBookPresenter;
    private SearchBookResultAdapter searchBookResultAdapter;
    private int type = 0;
    private int page = 1;
    private String searchBook = "";

    @Override
    protected int loadLayout() {
        return R.layout.activity_search_book;
    }

    @Override
    protected int setStatusBarColor() {
        return R.color.color_f7f8fa;
    }

    @Override
    protected void initData() {
        dataBinding.viewTitleBar.tvTitle.setText("搜索书籍");
        requestInterface();
        initSearchBookResult();
    }

    private void initSearchBookResult() {
        dataBinding.recSearchBookResult.setLayoutManager(new GridLayoutManager(this, 3));
        searchBookResultAdapter = new SearchBookResultAdapter();
        searchBookResultAdapter.setOnItemClickListener((view, position) -> {

        });
        dataBinding.recSearchBookResult.setAdapter(searchBookResultAdapter);
    }

    @Override
    protected void setListener() {
        dataBinding.viewTitleBar.ivBack.setOnClickListener(view -> {
            finish();
        });
        dataBinding.etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    searchBook = textView.getText().toString();
                    if (!TextUtils.isEmpty(searchBook)) {
                        type = 0;
                        searchBookPresenter.searchBook(searchBook, page, Constant.PAGE_SIZE);
                    } else {
                        showMessage("请输入要搜索的书籍");
                    }
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (null != inputMethodManager) {
                        inputMethodManager.hideSoftInputFromWindow(textView.getWindowToken(), 0);
                    }
                    return true;
                }
                return false;
            }
        });

        dataBinding.smartRefresh.setOnRefreshListener(refreshLayout -> {
            type = 1;
            page = 1;
            searchBook = "";
            dataBinding.etSearch.setText("");
            requestInterface();
        });
        dataBinding.smartRefresh.setOnLoadMoreListener(refreshLayout -> {
            type = 2;
            page++;
            requestInterface();
        });
    }

    private void requestInterface() {
        searchBookPresenter = new SearchBookPresenter(this);
        searchBookPresenter.searchBook(searchBook, page, Constant.PAGE_SIZE);
    }

    @Override
    public void success(List<SearchBookBean> searchBookBeans) {
        if (0 != searchBookBeans.size()) {
            if (0 == type) {
                searchBookResultAdapter.setData(searchBookBeans);
            } else if (1 == type) {
                searchBookResultAdapter.setData(searchBookBeans);
                dataBinding.smartRefresh.finishRefresh();
            } else {
                searchBookResultAdapter.setNewData(searchBookBeans);
                dataBinding.smartRefresh.finishLoadMore();
            }
        } else {
            if (0 == type ) {
                showMessage("暂无数据");
            }else if (1==type){
                showMessage("暂无数据");
                dataBinding.smartRefresh.finishRefresh();
            }else {
                dataBinding.smartRefresh.finishLoadMoreWithNoMoreData();
            }
        }
    }

    @Override
    public void failed(String message) {
        showMessage(message);
    }
}
